#include "BulletManager.h"

BulletManager::BulletManager()
{
    image = new CSprite();
    image->loadSprite("data/img/bullet.png", 3, 3, 0, 0, 0, 0, 1, 1, 1);
}

BulletManager::~BulletManager()
{
    //dtor
}

void BulletManager::addBullet(irrklang::vec3df& vel, irrklang::vec3df& pos)
{
    Bullet* bullet = new Bullet();
    bullet->pos = pos;

    bullet->vel = vel.normalize();
    bullet->vel = bullet->vel * 20;

    bullets.push_back(bullet);
}


// atualiza a velocidade do projetil
void BulletManager::update()
{
    for(int i = 0; i < bullets.size(); i++)
        bullets.at(i)->pos += bullets.at(i)->vel;
}

void BulletManager::draw()
{
    for(int i = 0; i < bullets.size(); i++) {
        // definir rotacao do projetil
        //image->setRotation(10);
        image->setPosition(bullets.at(i)->pos.X, bullets.at(i)->pos.Y);
        image->draw();
    }
}

bool BulletManager::hitTest(irrklang::vec3df& pos, float width, float height)
{
    float distance = 0;
    irrklang::vec3df center = pos;
    irrklang::vec3df centerBullet;
    center.X += width/2;
    center.Y += height/2;

    for(int i = 0; i < bullets.size(); i++)
    {
        centerBullet = bullets.at(i)->pos;
        centerBullet.X += 3;
        centerBullet.Y += 2;

        distance = center.getDistanceFrom(centerBullet);

        if(distance < width/2)
        {
            bullets.erase(bullets.begin() + i);
            return true;
        }
    }
    return false;
}
